package com.kaolafm.kradio.k_navi_map.basemvp;

import android.content.Intent;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Fragment基类<br/>
 * 已经在该基类中添加ButterKnife绑定.<br/>
 * 如果要使用EventBus只要{@link #useEventBus()}返回true。默认是不使用。<br/>
 *
 * @author donald
 * @date 2018/4/13
 */

public abstract class BaseFragment<P extends IPresenter> extends Fragment
        implements IFragment {

    protected P mPresenter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view;
        //如果通过布局id找不到view，就尝试直接获取view。
        try {
            view = inflater.inflate(getLayoutId(), container, false);
        } catch (NotFoundException e) {
            view = getFragmentView();
        }
        initData();
        mPresenter = createPresenter();
        initView(view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void initData() {

    }

    protected View getFragmentView() {
        return null;
    }

    /**
     * 获取fragment的布局id
     *
     * @return
     */
    protected abstract int getLayoutId();

    /**
     * 创建当前view对应的presenter
     *
     * @return
     */
    protected abstract P createPresenter();


    @Override
    public boolean useEventBus() {
        return false;
    }


    protected void showLoading() {

    }

    protected void hideLoading() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.destroy();
        }
        mPresenter = null;
    }
}
